package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.constants.SystemConstant;
import cn.stylefeng.guns.modular.gridsystem.entity.BizChannelType;
import cn.stylefeng.guns.modular.gridsystem.enums.LogExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.enums.SystemParamExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizChannelTypeMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizChannelTypeParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizChannelTypeResult;
import cn.stylefeng.guns.modular.gridsystem.service.BizChannelTypeService;
import cn.stylefeng.guns.modular.gridsystem.utils.OptionalBean;
import cn.stylefeng.guns.sys.modular.app.enums.SysAppExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 通道类型 服务实现类
 *
 * @author ssy
 * @date 2022/02/16 10:59
 */
@Service
public class BizChannelTypeServiceImpl extends ServiceImpl<BizChannelTypeMapper, BizChannelType> implements BizChannelTypeService {

    /**
     * 分页查询通道类型
     *
     * @param bizChannelTypeParam 查询条件入参
     *                            code    通道类型编码
     *                            name    类型名称
     *                            model   型号
     * @return Page<BizAmpLogResult>
     */
    @Override
    public PageResult<BizChannelType> page(BizChannelTypeParam bizChannelTypeParam) {
        OptionalBean.ofNullable(bizChannelTypeParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        // 构造条件
        LambdaQueryWrapper<BizChannelType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(bizChannelTypeParam.getCode()), BizChannelType::getCode, bizChannelTypeParam.getCode());
        queryWrapper.eq(StrUtil.isNotEmpty(bizChannelTypeParam.getName()), BizChannelType::getName, bizChannelTypeParam.getName());
        queryWrapper.eq(StrUtil.isNotEmpty(bizChannelTypeParam.getModel()), BizChannelType::getModel, bizChannelTypeParam.getModel());
        // 查询分页结果
        return new PageResult<>(page(PageFactory.defaultPage(), queryWrapper));
    }

    /**
     * 添加通道类型
     *
     * @param bizChannelTypeParam 新增数据入参
     *                            code      网关id数组
     *                            name      网关编号
     *                            fileName  通道
     *                            remark    网关编号
     *                            model     类型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizChannelTypeParam bizChannelTypeParam) {
        // 校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(bizChannelTypeParam, false);
        // 将dto转为实体
        BizChannelType bizChannelType = new BizChannelType();
        BeanUtil.copyProperties(bizChannelTypeParam, bizChannelType);
        save(bizChannelType);
    }

    /**
     * 根据id删除通道类型
     *
     * @param bizChannelTypeParam 删除入参
     *                            id     主键id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizChannelTypeParam bizChannelTypeParam) {
        removeById(bizChannelTypeParam.getId());
    }


    /**
     * 修改通道类型信息
     *
     * @param bizChannelTypeParam 修改日志信息入参
     *                            id            主键id
     *                            code          通道类型编码
     *                            name          类型名称
     *                            fileName      图片
     *                            remark        备注
     *                            model         型号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizChannelTypeParam bizChannelTypeParam) {
        BizChannelType bizChannelType = new BizChannelType();
        // 校验参数，检查是否存在重复的编码或者名称，排除当前编辑的这条记录
        checkParam(bizChannelTypeParam, true);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizChannelTypeParam, bizChannelType);
        bizChannelType.setUpdateUser(LoginContextHolder.me().getSysLoginUser().getId());
        bizChannelType.setUpdateTime(new Date());
        updateById(bizChannelType);
    }

    /**
     * 查看详情通道类型
     *
     * @param bizChannelTypeParam 查询入参
     *                            id      主键id
     * @return BizChannelTypeResult
     */
    @Override
    public BizChannelTypeResult detail(BizChannelTypeParam bizChannelTypeParam) {
        return queryBizChannelType(bizChannelTypeParam);
    }


    /**
     * 校验参数，检查是否存在重复的编码等
     *
     * @param bizChannelTypeParam 校验参数
     *                            id    主键id
     *                            code  被校验code
     * @param isExcludeSelf       是否排除本身
     *                            true/false
     */
    private void checkParam(BizChannelTypeParam bizChannelTypeParam, boolean isExcludeSelf) {
        OptionalBean.ofNullable(bizChannelTypeParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        OptionalBean.ofStr(bizChannelTypeParam.getId());
        OptionalBean.ofStr(bizChannelTypeParam.getCode());
        // 构建带code的查询条件
        LambdaQueryWrapper<BizChannelType> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(BizChannelType::getCode, bizChannelTypeParam.getCode());
        // 如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByCode.ne(BizChannelType::getId, bizChannelTypeParam.getId());
        }
        if (count(queryWrapperByCode) >= SystemConstant.NUM_1) {
            throw new ServiceException(SysAppExceptionEnum.APP_CODE_REPEAT);
        }
    }


    /**
     * 根据id获取通道类型
     *
     * @param bizChannelTypeParam 查询入参
     *                            id      主键id
     * @return BizChannelTypeResult
     */
    private BizChannelTypeResult queryBizChannelType(BizChannelTypeParam bizChannelTypeParam) {
        OptionalBean.ofNullable(bizChannelTypeParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        OptionalBean.ofStr(bizChannelTypeParam.getId());
        BizChannelType bizChannelType = getById(bizChannelTypeParam.getId());
        OptionalBean.ofNullable(bizChannelType).orElseThrow(() -> new ServiceException(LogExceptionEnum.NOT_EXISTED));
        BizChannelTypeResult bizChannelTypeResult = new BizChannelTypeResult();
        BeanUtil.copyProperties(bizChannelType, bizChannelTypeResult);
        return bizChannelTypeResult;
    }
}
